package database

import (
	log2 "chess/log"
	"chess/tools"
	"context"
	"errors"
	"fmt"
	"github.com/go-redis/redis/v8"
	"strconv"
)

func IsPveGame(ctx context.Context, roomId int, userId int) (isPveGame bool, err error) {
	redisPath := tools.GetEnv("REDIS_PATH", "localhost:6379")
	redisPD := tools.GetEnv("REDIS_PASSWORD", "")
	rdb := redis.NewClient(&redis.Options{
		Addr:     redisPath,
		Password: redisPD, // no password set
		DB:       1,       // use default DB
		//PoolSize: 500, // 连接池大小
	})
	result1, err1 := rdb.SIsMember(ctx, strconv.Itoa(roomId), userId).Result()
	result2, err2 := rdb.SIsMember(ctx, strconv.Itoa(roomId), "AlphaGo").Result()
	if err1 != nil {
		writeLog, err3 := log2.WriteLog(context.Background(), "chessgame", "IsPveGame", strconv.Itoa(roomId), "Warring", err1.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err3 != nil {
			fmt.Println("写入日志失败")
		}
		return false, err1
	}
	if err2 != nil {
		writeLog, err3 := log2.WriteLog(context.Background(), "chessgame", "IsPveGame", strconv.Itoa(roomId), "Warring", err2.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err3 != nil {
			fmt.Println("写入日志失败")
		}
		return false, err2
	}
	if result1 == false {
		writeLog, err3 := log2.WriteLog(context.Background(), "chessgame", "IsPveGame", strconv.Itoa(roomId), "Warring", "User is not in the room")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err3 != nil {
			fmt.Println("写入日志失败")
		}
		return false, errors.New("user is not in the room")
	}
	if result1 == true && result2 == true {
		writeLog, err3 := log2.WriteLog(context.Background(), "chessgame", "IsPveGame", strconv.Itoa(roomId), "Debug", "This game is a PVE game")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err3 != nil {
			fmt.Println("写入日志失败")
		}
		return true, err
	}
	if result1 == true && result2 == false {
		writeLog, err3 := log2.WriteLog(context.Background(), "chessgame", "IsPveGame", strconv.Itoa(roomId), "Debug", "This game is a PVP game")
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err3 != nil {
			fmt.Println("写入日志失败")
		}
		return false, err
	}
	return false, err
}
